package team.kirohuji.service.system;

import java.util.List;
import java.util.Optional;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;

import team.kirohuji.domain.system.UrlFilter;
import team.kirohuji.repository.jpa.system.UrlFilterRepository;
import team.kirohuji.shiro.core.ShiroFilerChainManager;

public class UrlFilterServiceImpl implements UrlFilterService {
	@Autowired
	private ShiroFilerChainManager shiroFilerChainManager;

	@Autowired
	private UrlFilterRepository urlFilterRepository;

	@Override
	public UrlFilter createUrlFilter(UrlFilter urlFilter) {
		urlFilterRepository.save(urlFilter);
		initFilterChain();
		return urlFilter;
	}

	@Override
	public UrlFilter updateUrlFilter(UrlFilter urlFilter) {
		return createUrlFilter(urlFilter);
	}

	@Override
	public void deleteUrlFilter(Integer urlFilterId) {
		urlFilterRepository.deleteById(urlFilterId);
		initFilterChain();
	}

	@Override
	public Optional<UrlFilter> findOne(Integer urlFilterId) {
		return urlFilterRepository.findById(urlFilterId);
	}

	@Override
	public List<UrlFilter> findAll() {
		return urlFilterRepository.findAll();
	}

	@PostConstruct
	public void initFilterChain() {
		shiroFilerChainManager.initFilterChains(findAll());
	}
}